// firstPackage/pages/msgModule/recommendMsgList/recommendMsgList.js

import { queryRecommendMsgList, batchReadRecommendMsg } from "../../../../service/msg_service"

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    recommendList: [

    ],
    page: 0,
    pageSize: 10,
    scrollRefreshStatus: true,
    isShowLoading: false,
    isBottomShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryRecommendMsgList();
    this.batchReadRecommendMsg();
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  queryRecommendMsgList() {
    wx.showLoading();
    let pageNum = this.data.page;
    let pageSize = this.data.pageSize;
    queryRecommendMsgList(pageNum, pageSize).then(res => {
      wx.hideLoading();
      this.setData({
        scrollRefreshStatus: false,
      })
      console.log("JJLog:报备消息列表:" + JSON.stringify(res));
      if (res.code == 200) {
        let tempArr = [];
        tempArr = this.data.recommendList;
        if (tempArr.length > 0 && pageNum > 0) {
          //加载下一页:
          tempArr = tempArr.concat(res.data);
          if (res.data.length < pageSize) {
            this.setData({
              isShowLoading: false,
              isBottomShow: true,
            })
            this.setData({
              recommendList: tempArr
            })
          } else if (res.data.length == pageSize) {
            this.setData({
              isShowLoading: false,
            })
            this.setData({
              recommendList: tempArr
            })
          }
        } else {
          this.setData({
            isShowLoading: false,
          })
          this.setData({
            recommendList: res.data
          })
          if (res.data.length < pageSize) {
            this.setData({
              isBottomShow: true
            })
          }
        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      this.setData({
        scrollRefreshStatus: false,
      })

      let page = this.data.page;
      if (page > 0) {
        page = page - 1;
      }
      this.setData({
        isShowLoading: false,
        page: page
      })
      console.error("JJLog:queryRecommendMsgList", err);
    })
  },
  batchReadRecommendMsg() {
    batchReadRecommendMsg().then(res => {
      if (res.code == 200) {

      }
    }).catch(err => {
      console.error("JJLog:batchReadRecommendMsg", err);
    })
  },

  pushToDetail(e) {
    let id = e.currentTarget.dataset.id;
    let type = e.currentTarget.dataset.type;
    if (type == 1) {
      wx.navigateTo({
        url: '/firstPackage/pages/reportModule/reportRecordDetail/reportRecordDetail?id=' + id,
      })
    } else if (type == 2) {
      //报备管理者页面
      wx.navigateTo({
        url: '/firstPackage/pages/reportModule/reportManage/reportManage',
      })
    }

  },

  callPhoneAction(e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
      fail: err => {

      }
    })
  },

  //复制信息
  copySysInfoAction(e) {
    let item = e.currentTarget.dataset.item;
    console.log("JJLog:报备信息是:" + JSON.stringify(item));

    let copyText = "";
    let companyName = item.companyName ? item.companyName : '自由经纪人';
    copyText = "报备项目：" + item.houseName + "\n";//楼盘名称
    copyText = copyText + '报备公司：' + companyName + "\n";//物业类型
    copyText = copyText + '报备人姓名：' + item.recommendName + '\n';
    copyText = copyText + '报备人电话：' + item.recommendPhone + '\n';
    copyText = copyText + '客户姓名：' + item.customerName + "\n";//区域
    copyText = copyText + '客户电话：' + item.customerPhone + "\n";//参考价
    copyText = copyText + '预计到访时间：' + item.visitingTimeAt + '\n';

    wx.setClipboardData({
      data: copyText,
      success: (res) => {
        wx.getClipboardData({
          success: (res) => {
            wx.showToast({
              title: '复制信息成功'
            })
          },
          fail: (err) => {
            wx.showToast({
              title: '复制失败',
              icon: "none"
            })
          }
        })
      }
    })
  },


  //到底继续加载
  scrolltolower() {
    if (this.data.isBottomShow) {
      //已经到底了就不加载了
      return;
    }
    let tempPage = this.data.page + 1;
    this.setData({
      page: tempPage,
      isShowLoading: true,
    })
    this.queryRecommendMsgList();
  },

  //下拉刷新:
  refreshDownData() {
    console.log("JJLog:下拉刷新来了=====:");
    this.setData({
      scrollRefreshStatus: true,
      page: 0,
      isBottomShow: false,
    })
    this.queryRecommendMsgList();
  },






})